package com.jhws.core.log.controller;

import com.jhws.common.base.bo.JsonRespData;
import com.jhws.common.PropertiesReader;
import com.jhws.core.log.service.ILogService;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/log")
public class LogCtrl {

    private String logStore = PropertiesReader.getProperty("logAliLogStoreForCall");
    @Resource
    ILogService ILogService;

    @RequestMapping("callLogs.x")
    @ResponseBody
    public JsonRespData callLogs(Integer timeStart, Integer timeEnd,String commCode,Integer sliseMode) throws Exception{
        if((timeEnd == null || timeEnd == 0) && (timeStart == null || timeStart == 0)){
            Long timeSec = (new Date()).getTime()/1000;
            timeStart = timeSec.intValue() - 3600*24*30;
            timeEnd = timeSec.intValue();
        }
        Map map = ILogService.getLogsCount(logStore,"",commCode,timeStart,timeEnd, sliseMode);
        return new JsonRespData("",1,map);
    }

    @RequestMapping("callLogStatstics.x")
    @ResponseBody
    public JsonRespData callLogStatstics(Integer timeStart,Integer timeEnd,String commCode) throws Exception{
        String query = "*";
        String commQuery = "* and ";
        Map map = new HashMap();

        if((timeEnd == null || timeEnd == 0) && (timeStart == null || timeStart == 0)){
            Long timeSec = (new Date()).getTime()/1000;
            timeStart = timeSec.intValue() - 3600*24*30;
            timeEnd = timeSec.intValue();
        }

        if(StringUtils.isNotEmpty(commCode))
            commQuery = "caller ="+commCode+"???????? and ";

        query = commQuery + query;
        long callCount = ILogService.getLogsTotalCount(logStore,"",timeStart,timeEnd,query);
        map.put("totalCount",callCount);

        query = commQuery+ "time_answer > 0 ";
        long callAnswered = ILogService.getLogsTotalCount(logStore,"",timeStart,timeEnd,query);
        map.put("callAnswered",callAnswered);

        query = commQuery+"hangup_reason = UNALLOCATED_NUMBER";
        long hangup_not_found = ILogService.getLogsTotalCount(logStore,"",timeStart,timeEnd,query);
        map.put("hangupNotFound",hangup_not_found);

        query = commQuery+"hangup_reason = ORIGINATOR_CANCEL";
        long hangup_cancel = ILogService.getLogsTotalCount(logStore,"",timeStart,timeEnd,query);
        map.put("hangupCancel",hangup_cancel);

        query = commQuery+"hangup_reason = CALL_REJECTED";
        long rejected = ILogService.getLogsTotalCount(logStore,"",timeStart,timeEnd,query);
        map.put("rejected",rejected);

        query = commQuery+"hangup_reason = USER_BUSY";
        long busy = ILogService.getLogsTotalCount(logStore,"",timeStart,timeEnd,query);
        map.put("busy",busy);

        return new JsonRespData("",1,map);
    }

}
